﻿/*
Copyright (c) 2007-2011 William POTTIER - AllProgrammic

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights 
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
of the Software, and to permit persons to whom the Software is furnished to do so, 
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all 
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
using System;
using System.IO;

namespace NetAccess.Formatters
{
    /// <summary>
    /// Classe généric pour des formatter de type
    /// </summary>
    /// <typeparam name="T">Type géré</typeparam>
    public abstract class TypeFormatterGeneric<T> : ITypeFormatter
    {
        private MessageFormatter _messageFormatter;

        #region ITypeFormatter Members
        /// <summary>
        /// Permet au gestionnaire de s'enregistré auprès du formatter
        /// </summary>
        /// <param name="mf">Gestionnaire</param>
        public void RegisterMessageFormatter(MessageFormatter mf)
        {
            _messageFormatter = mf;
        }

        /// <summary>
        /// Obtiens le type géré par ce formatter
        /// </summary>
        /// <returns>Le type géré</returns>
        public Type FormatterOfType()
        {
            return typeof(T);
        }

        /// <summary>
        /// Format un objet
        /// </summary>
        /// <param name="stream">flux binaire utilisé pour le formatage</param>
        /// <param name="data">donnée à formatter</param>
        public void Format(ref BinaryWriter stream, object data)
        {
            if (data.GetType().Equals(FormatterOfType()))
                FormatData(ref stream, (T)data);
            else
            {
                try
                {
                    // On tente de Boxer le type
                    FormatData(ref stream, (T)data);
                }
                catch (Exception ex)
                {
                    throw new ArgumentException("Le type de l'objet passé ne correspond pas au formatter.", "data", ex);
                }
            }
                
        }

        /// <summary>
        /// Deformat un objet
        /// </summary>
        /// <param name="stream">flux de donnée à déformater</param>
        /// <returns>l'objet déformatté</returns>
        public object UnFormat(ref BinaryReader stream)
        {
            return UnFormatData(ref stream);
        }

        #endregion

        /// <summary>
        /// Specialise le formatage d'un type
        /// </summary>
        /// <param name="stream">flux</param>
        /// <param name="data">objet</param>
        public abstract void FormatData(ref BinaryWriter stream, T data);

        /// <summary>
        /// Specialise le déformatage d'un objet
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public abstract T UnFormatData(ref BinaryReader stream);

        /// <summary>
        /// Permet le formatage d'un sous objet
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="data"></param>
        protected void FormatObject(ref BinaryWriter stream, object data)
        {
            _messageFormatter.FormatObject(ref stream, data);
        }

        /// <summary>
        /// Permet le formatage d'un sous tableau
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="data"></param>
        protected void FormatArray(ref BinaryWriter stream, object[] data)
        {
            _messageFormatter.FormatArray(ref stream, data);
        }

        /// <summary>
        /// Permet le déformatage d'un sous objet
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        protected object UnFormatObject(ref BinaryReader stream)
        {
            return _messageFormatter.UnFormatObject(ref stream);
        }
        
        /// <summary>
        /// Permet le déformatage d'un sous tableau
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        protected object[] UnFormatArray(ref BinaryReader stream)
        {
            return _messageFormatter.UnFormatArray(ref stream);
        }
}
}
